package edu.dartmouth.cs.gae_sample;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.dartmouth.cs.gae_sample.data.Contact;
import edu.dartmouth.cs.gae_sample.data.ContactDatastore;

public class QueryServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
		String vec[] = new String[35];
		for(int i = 0; i <35; i++){
			
			vec[i] = "0";
		}
		
		String id = req.getParameter("id");
		
		//req.getParameterNames("");
		// Verificar y guardar cada chekbox seleccionado (ok)
		// Vector Con 1 (selected) 0 (No Selected)  (ok)
		// Vamos al metodo query para llenar el ArrayList
		//System.out.println(req.getParameter("cId"));
		if(req.getParameter("cId") != null) {  vec[0] = "1";	}
		if(req.getParameter("cTema") != null) {  vec[1] = "1";	}
		if(req.getParameter("cTitulo") != null) {  vec[2] = "1";	}
		if(req.getParameter("cResumen") != null) {  vec[3] = "1";	}
		if(req.getParameter("cAutorind") != null) {  vec[4] = "1";	}
		if(req.getParameter("cAutor1") != null) {  vec[5] = "1";	}
		if(req.getParameter("cAutor2") != null) {  vec[6] = "1";	}
		if(req.getParameter("cAutor3") != null) {  vec[7] = "1";	}
		if(req.getParameter("cAutor4") != null) {  vec[8] = "1";	}
		if(req.getParameter("cAutor5") != null) {  vec[9] = "1";	}
		if(req.getParameter("cAutor6") != null) {  vec[10] = "1";	}
		if(req.getParameter("cAfiliacion") != null) {  vec[11] = "1";	}
		if(req.getParameter("cUnal") != null) {  vec[12] = "1";	}
		if(req.getParameter("cInstitucion") != null) {  vec[13] = "1";	}
		if(req.getParameter("cCargo") != null) {  vec[14] = "1";	}
		if(req.getParameter("cDia") != null) {  vec[15] = "1";	}
		if(req.getParameter("cMes") != null) {  vec[16] = "1";	}
		if(req.getParameter("cAnio") != null) {  vec[17] = "1";	}
		if(req.getParameter("cLugar") != null) {  vec[18] = "1";	}
		if(req.getParameter("cFecha") != null) {  vec[19] = "1";	}
		if(req.getParameter("cNombre") != null) {  vec[20] = "1";	}
		if(req.getParameter("cEditorial") != null) {  vec[21] = "1";	}
		if(req.getParameter("cUbicacion") != null) {  vec[22] = "1";	}
		if(req.getParameter("cAcceso") != null) {  vec[23] = "1";	}
		if(req.getParameter("cImportancia") != null) {  vec[24] = "1";	}
		if(req.getParameter("cObserv") != null) {  vec[25] = "1";	}
		if(req.getParameter("cAmbiental") != null) {  vec[26] = "1";	}
		if(req.getParameter("cBienestar") != null) {  vec[27] = "1";	}
		if(req.getParameter("cDemocracia") != null) {  vec[28] = "1";	}
		if(req.getParameter("cFormacion") != null) {  vec[29] = "1";	}
		if(req.getParameter("cInvest") != null) {  vec[30] = "1";	}
		if(req.getParameter("cLegado") != null) {  vec[31] = "1";	}
		if(req.getParameter("cMegat") != null) {  vec[32] = "1";	}
		if(req.getParameter("cNalPub") != null) {  vec[33] = "1";	}		
		if(req.getParameter("cTerrit") != null) {  vec[34] = "1";	}			
		
		req.setAttribute("vec", vec);
		
		ArrayList<Contact> result = ContactDatastore.query(id,vec);
		
		//System.out.println(result);
		
		req.setAttribute("result", result);
		getServletContext().getRequestDispatcher("/query_result.jsp").forward(
				req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		doGet(req, resp);
	}
}
